import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import {baseDataStatus} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import DateRangePicker from 'Shared/components/DateRangePicker';
import WrappedSelect from '../common/WrappedSelect';
import TagSelect from 'Shared/components/TagSelect';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import {hasPermissions} from './utils';
import {injectIntl, language} from './intl';
import ImportPanel from './ImportPanel';
const FormItem = Form.Item;

const baseDataStatusList = baseDataStatus.toList(language);
class QueryPanel extends React.PureComponent {
    state = {
        loading: false,
        showImportPanel: false
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    onChangeImport = () => {
        this.setState({
            showImportPanel: true
        });
    }
    onCloseImportPanel = () => {
        this.setState({
            showImportPanel: false
        });
    }
    onImportChange = data => {
        if(data)
            this.setState({
                showImportPanel: false
            });
    }
    render() {
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        const otherBtns = [];
        if(hasPermissions(this.props.permissions, PERMISSION.import))
            otherBtns.push(<Button key="import" onClick={this.onChangeImport}>{this.props.intl.formatMessage({
                id: 'queryPanel.btn.import',
                defaultMessage: '导入'
            })}</Button>);
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerId',
                                defaultMessage: '总代'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DealerSelectPanel
                                value={dealerInfo}
                                onSelect={this.onSelectDealer} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.brandId',
                                defaultMessage: '品牌'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                allowClear
                                name="brandId"
                                value={this.props.conditions.brandId}
                                options={this.props.initEnum.brands}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.code',
                                defaultMessage: '出运单号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="code"
                                value={this.props.conditions.code}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.saleShipmentCode',
                                defaultMessage: '发运单号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="saleShipmentCode"
                                value={this.props.conditions.saleShipmentCode}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.boardDate',
                                defaultMessage: '装船日期'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DateRangePicker name="boardDate" value={this.props.conditions.boardDate}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.createTime',
                                defaultMessage: '创建日期'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DateRangePicker name="createTime" value={this.props.conditions.createTime}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    
                </Row>
                <Row>
                    <Col>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.transportStatus',
                                defaultMessage: '状态'
                            })}
                            {...FORM_ROW_OPTIONS.item}>
                            <TagSelect
                                name="transportStatus"
                                value={this.props.conditions.transportStatus}
                                options={baseDataStatusList}
                                onChange={this.handleFilterChange} />

                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.props.onResetClick}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {otherBtns}
                    </Col>
                </Row>
                {
                    this.state.showImportPanel &&
                        <ImportPanel
                            selectKey={'1'}
                            onCancel={this.onCloseImportPanel}
                            onImportChange={this.onImportChange} />
                }
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    initEnum: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    saveQueryDealer
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';

const getInitData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    initEnum: getInitData(state),
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
    permissions: getPermissions(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel())
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
